package com.tigercel.xiangchengyun.ui.activities

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.GridView
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.adapter.HotVideoAdapter
import com.tigercel.xiangchengyun.entity.Banner
import com.tigercel.xiangchengyun.entity.HotVideoEntity
import com.tigercel.xiangchengyun.utils.Global
import com.tigercel.xiangchengyun.utils.JsonUtil
import com.tigercel.xiangchengyun.utils.PreferencesUtil

import kotlinx.android.synthetic.main.include_titlebar.*
import org.jetbrains.anko.toast

class MoreViewsActivity : AppCompatActivity() {
    private var channel_id: String? = null
    private var title: String? = null
    private var mSmartRefreshLayout: SmartRefreshLayout? = null
    private var mGridView: GridView? = null
    private lateinit var VideoList: MutableList<HotVideoEntity>
    private var VideoAdapter: HotVideoAdapter? = null
    private var localDatas: PreferencesUtil? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_more_views)
        mSmartRefreshLayout = findViewById(R.id.refresh_more_view)
        mGridView = findViewById(R.id.more_grid_view)
        channel_id = intent.getStringExtra("channel_id")
        title = intent.getStringExtra("title")
        localDatas = PreferencesUtil(this)
        mSmartRefreshLayout!!.autoRefresh()
        mSmartRefreshLayout!!.setOnRefreshListener { getContent(channel_id!!) }
        mSmartRefreshLayout!!.setOnLoadMoreListener { getMore(channel_id!!) }
        titlebar_tv.text = intent.getStringExtra("title")
        titlebar_iv_left.setImageDrawable(resources.getDrawable(R.drawable.btn_nav_back))
        titlebar_iv_left.visibility = View.VISIBLE
        titlebar_iv_left.setOnClickListener(View.OnClickListener { this.finish() })
    }
    private fun getContent(channel_id: String) {
        OkGo.get<String>(Global.baseUrl + Global.commonUrl)
                .tag(this)
                .params("channel_id", channel_id)
                .params("device_code", Global.getDeviceId())
                .params("user_id", localDatas!!.preferences["user_id"])
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>) {
                        val result = response!!.body()
                        val status = JsonUtil.get_key_string("status", result!!)
                        val msg = JsonUtil.get_key_string("msg", result!!)
                        val code = JsonUtil.get_key_string("code", result!!)
                        val data = JsonUtil.get_key_string("data", result!!)
                        if (status == "0") {
                            val model = JsonUtil.get_key_string("model", data)
                            val show_type = JsonUtil.get_key_string("show_type", data)
                            val banners = JsonUtil.getList("banner", data)
                            val content = JsonUtil.getList("content", data)
                            if (banners.isNotEmpty()) {
//                                    refresh_layout.addView(headBanner)
                                var banners1: MutableList<Banner>
                                banners1 = ArrayList()
                                banners1.clear()
                                for (i in 0 until banners.size) {
                                    var img = JsonUtil.get_key_string("img", banners[i])
                                    var url = JsonUtil.get_key_string("url", banners[i])
                                    var name = JsonUtil.get_key_string("title", banners[i])
                                    var bannerBean = Banner(i, name, img, url)
                                    banners1.add(bannerBean)
                                }
//                                    banner!!.setAdapter(NewsBannerAdapter(applicationContext), banners1 as List<Any>?)
                            }
                            VideoList = ArrayList()
                            for (i in 0 until content.size) {
                                var content_id = JsonUtil.get_key_string("content_id", content[i])
                                var title = JsonUtil.get_key_string("title", content[i])
                                var thumb_img = JsonUtil.get_key_string("thumb_img", content[i])
                                var url = JsonUtil.get_key_string("url", content[i])
                                var pub_time = JsonUtil.get_key_string("pub_time", content[i]).toLong()
                                var hotVideoEntity = HotVideoEntity(content_id, title, thumb_img, url, pub_time)
                                VideoList!!.add(hotVideoEntity)
                            }
                            VideoAdapter = HotVideoAdapter(this@MoreViewsActivity, VideoList)
                            mGridView!!.adapter = VideoAdapter
                            mSmartRefreshLayout!!.finishRefresh()
                        } else {
                            toast(getString(R.string.readFaile) )
                        }
                    }
                })
    }

    private fun getMore(channel_id: String) {
        OkGo.get<String>(Global.baseUrl + Global.commonUrlMore)
                .tag(this)
                .params("channel_id", channel_id)
                .params("device_code", Global.getDeviceId())
                .params("page_size", "10")
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>) {
                        val result = response!!.body()
                        val status = JsonUtil.get_key_string("status", result!!)
                        val msg = JsonUtil.get_key_string("msg", result!!)
                        val code = JsonUtil.get_key_string("code", result!!)
                        val data = JsonUtil.getList("data", result!!)
                        if (status == "0") {
                            var VideoList1: MutableList<HotVideoEntity> = ArrayList()
                            if (data.isEmpty()) {
                                mSmartRefreshLayout!!.finishLoadMoreWithNoMoreData()
                            } else {
                                for (i in 0 until data.size) {
                                    var content_id = JsonUtil.get_key_string("content_id", data[i])
                                    var title = JsonUtil.get_key_string("title", data[i])
                                    var thumb_img = JsonUtil.get_key_string("thumb_img", data[i])
                                    var url = JsonUtil.get_key_string("url", data[i])
                                    var pub_time = JsonUtil.get_key_string("pub_time", data[i]).toLong()
                                    var hotVideoEntity = HotVideoEntity(content_id, title, thumb_img, url, pub_time)
                                    VideoList1!!.add(hotVideoEntity)
                                }
                                VideoAdapter!!.addAll(VideoList1)
                                mSmartRefreshLayout!!.finishLoadMore()
                            }

                        } else {
                            toast(getString(R.string.readFaile) )
                        }
                    }
                })
    }
}
